/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.anhndt.audiobook.entity;

import com.anhndt.audiobook.utils.StringUtil;
import com.anhndt.audiobook.utils.Util;
import com.sun.lwuit.Image;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

/**
 *
 * @author Sun
 */
public class ItemEntity {

    public static final String ID = "i";
    public static final String NAME = "n";
    public static final String DESCRIPTION = "d";
    public static final String COUNTER_CHILD = "c";
    public static final String THUMBNAIL = "t";
    public static final String MARKED = "m";
    public static final String RATE = "r";
    public static final String URL = "u";
    public static final String CONTENT_TYPE = "y";
    public static final String OTHER = "o";
    //Type entity
    public static final byte TYPE_BLOCKED_REQUEST = 0;
    public static final byte TYPE_CATEGORY = 1;
    public static final byte TYPE_ARTICLE = 2;
    public static final byte TYPE_ARTICLE_PART = 3;
    public static final byte TYPE_HIT = 4;
    public static final byte TYPE_FAVORITE_LIST = 5;
    public static final byte TYPE_FAVORITE_ADD = 6;
    public static final byte TYPE_FAVORITE_DELETE = 7;
    public static final byte TYPE_FAVORITE_DELETE_ALL = 8;
    public static final byte TYPE_DOWNLOAD = 9;
    public static final byte TYPE_RATING = 10;
    public static final byte TYPE_NOTIFY = 11;
    public static final byte TYPE_COUNTER_ITEM = 12;
    public static final byte TYPE_USER_LOGIN = 13;
    public static final byte TYPE_USER_CHANGE_PASS = 14;
    public static final byte TYPE_USER_INFO = 15;
    public static final byte TYPE_USER_REGISTER = 16;
    public static final byte TYPE_USER_GET_PASSWORD = 17;
//    public static final byte TYPE_THEMES = 20;
//    public static final byte TYPE_THEMES_DETAIL = 21;
    public static final byte TYPE_SETTING = Byte.MAX_VALUE;
    public byte typeEntity = TYPE_CATEGORY;
    public String id, name, description, marked, url, other;
    int rate;
    public int counterChild;
    public Image icon;
    private Hashtable itemChildren;
    private ItemEntity parent;
    public boolean loaded; //default

    public ItemEntity() {
    }

    public ItemEntity(String id, String name, String des) {
        this(id, name, des, TYPE_CATEGORY);
    }

    public ItemEntity(String id, String name, String des, byte type) {
        this(id, name, des, null, null, 0, null, null, "" + type, 0);
    }

    public ItemEntity(String id, String name, String description, String thumbnail, String marked, int rate, String url, String other, String contentType, int counterChild) {
        this.id = id;
        this.name = StringUtil.isAnyType(name) ? null : name;
        this.description = description;
//        this.thumbnail = thumbnail;
        this.marked = marked;
        this.rate = rate;
        this.url = url;
        this.other = other;

        if (!StringUtil.isNullOrEmpty(contentType)) {
            typeEntity = Byte.parseByte(contentType);
        }
        this.counterChild = counterChild;
    }

    public void changeMarked(boolean marked) {
        this.marked = marked ? "1" : "0";
    }
    public int getRate() {
        return rate;
    }

    public Hashtable getItemChildren() {
        return itemChildren;
    }

    public ItemEntity getParent() {
        return parent;
    }

    public void setParent(ItemEntity parent) {
        this.parent = parent;
    }

    public void addChild(ItemEntity entity, String type) {
        Vector vt = getValue(type);
        entity.setParent(this);
        vt.addElement(entity);
    }

    public int indexOf(ItemEntity item, String type) {
        Vector vt = getValue(type);
        return vt.indexOf(item);
    }
//    public void setChild(Vector items, String type) {
//        Vector vt = getValue(type);
//        vt.removeAllElements();
//        vt = items;
//        int size = itemChildrends.size();
//        for (int i = 0; i < size; i++) {
//            ItemEntity entity = (ItemEntity) items.elementAt(i);
//            entity.setParent(this);
//        }
//    }

    public Vector getValue(String key) {
        if (itemChildren == null) {
            itemChildren = new Hashtable();
        }
        if (!itemChildren.containsKey(key)) {
            Vector vt = new Vector();
            itemChildren.put(key, vt);
        }
        Vector vt = (Vector) itemChildren.get(key);
        return vt;
    }

    public int size() {
        return itemChildren != null ? itemChildren.size() : 0;
    }

    public int size(String key) {
        if (StringUtil.isNullOrEmpty(key) || itemChildren == null || !itemChildren.containsKey(key)) {
            return 0;
        }
        return getValue(key).size();
    }

    public boolean containtType(String type) {
        try {
            if (itemChildren != null && itemChildren.containsKey(type)) {
                Vector vt = (Vector) itemChildren.get(type);
                return vt.size() > 0;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public Vector getItemChildrends(String type) {
        return getValue(type);
    }

    public void removeAllChilrends() {
        loaded = false;
        if (itemChildren != null) {
            itemChildren.clear();
            itemChildren = null;
        }
    }

    public void removeChild(String type) {
        if (itemChildren != null && itemChildren.containsKey(type)) {
            itemChildren.remove(type);
        }
    }

    public void removeAllIconOfChild(String type) {
        Vector vt = getItemChildrends(type);
        int size = vt.size();
        if (size == 0) {
            return;
        }

        System.out.println("remove icon  : " + size);
        ItemEntity entity;
        for (int i = 0; i < size; i++) {
            entity = (ItemEntity) vt.elementAt(i);
            entity.icon = null;
        }
    }

    public boolean hasThumbnail() {
        return typeEntity == TYPE_ARTICLE;
    }

    public boolean hasExpandIcon() {
        return typeEntity == TYPE_CATEGORY && counterChild < 0;
    }

    public int getTypeEntity() {
        return typeEntity;
    }

    public boolean hasTwoInfo() {
        return !StringUtil.isNullOrEmpty(name) && !StringUtil.isNullOrEmpty(description);
    }

    public boolean hasParentEntity() {
        return parent != null;
    }

    public boolean isMarked() {
        return marked != null && marked.equals("1");
    }

    public String getThumbUrl() {
        return StringUtil.format(Util.LOAD_IMAGE_URL, new String[]{"" + typeEntity, id});
    }

    public ItemEntity getFirstItem(String type) {
        if (itemChildren == null || !itemChildren.containsKey(type)) {
            return null;
        }
        Vector vt = getValue(type);
        return vt.size() > 0 ? (ItemEntity) vt.elementAt(0) : null;
    }

    public String getMainKey() {
        if (typeEntity == TYPE_CATEGORY && counterChild == 0) {
            return "" + TYPE_CATEGORY;
        } else if (typeEntity == TYPE_HIT || (typeEntity == TYPE_CATEGORY && counterChild > 0)) {
            return "" + TYPE_ARTICLE;
        } else if (typeEntity == TYPE_ARTICLE) {
            return "" + TYPE_ARTICLE_PART;
        } else if (typeEntity == TYPE_FAVORITE_LIST) {
            return "" + TYPE_ARTICLE;
        }
        return null;
    }

    public String toString() {
        return name;
    }

    public boolean equals(ItemEntity entity) {
        return (entity == null) || this.id.equals(entity.id);
    }

    public ItemEntity clone() {
        return new ItemEntity(id, name, description, name, marked, rate, url, other, "" + typeEntity, counterChild);
    }
}
